package argo.hot100.arr;

public class ProductExceptSelf {
    public int[] productExceptSelf(int[] nums) {
        if (nums == null || nums.length == 1) {
            return nums;
        }
        int[] res = new int[nums.length];

        int sub = 1;
        res[0] = 1;
        for (int i = 1; i < nums.length; i++) {
            sub *= nums[i - 1];
            res[i] = sub;
        }

        sub = 1;
        for (int i = nums.length - 1; i >= 0; i--) {
            res[i] *= sub;
            sub *= nums[i];
        }

        return res;
    }
}
